<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>课程管理</span>
      </div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="课程编号">
          <el-input v-model="searchForm.code" placeholder="请输入课程编号" clearable size="small"></el-input>
        </el-form-item>
        <el-form-item label="课程名称">
          <el-input v-model="searchForm.name" placeholder="请输入课程名称" clearable size="small"></el-input>
        </el-form-item>
        <el-form-item label="适用人群">
          <el-select v-model="searchForm.applicablePerson" placeholder="请选择适用人群" clearable size="small">
            <el-option label="小白学员" value="1"></el-option>
            <el-option label="中级程序员" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程学科">
          <el-select v-model="searchForm.subject" placeholder="请选择课程学科" clearable size="small">
            <!--            <el-option label="Java" value="0"></el-option>-->
            <!--            <el-option label="前端" value="1"></el-option>-->
            <!--            <el-option label="UI" value="2"></el-option>-->
            <!--            <el-option label="实施" value="3"></el-option>-->
            <!--            <el-option label="测试" value="4"></el-option>-->
            <el-option v-for="item in xkOption" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间：">
          <el-date-picker
              v-model="dateRange"
              size="small"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="doSearch">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card">
      <el-button type="primary" style="margin-bottom: 10px" @click="handleAdd">添加</el-button>
      <el-table :data="pageData.data" style="width: 100%" border>
        <el-table-column prop="code" label="课程编号" width="80px"></el-table-column>
        <el-table-column prop="subject" label="课程学科">
          <template slot-scope="scope">
            {{ getDictLabel(scope.row.subject, xkOption) }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="课程名称"></el-table-column>
        <el-table-column prop="price" label="价格(元)"></el-table-column>
        <el-table-column label="适用人群">
          <template slot-scope="scope">{{ scope.row.applicablePerson == 2 ? '小白学员' : '中级程序员' }}</template>
        </el-table-column>
        <el-table-column prop="info" label="课程介绍"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作" width="240px">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageData.pageNo"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageData.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageData.totalCount">
      </el-pagination>

      <el-dialog :title="saveForm.id==null?'新增课程':'修改课程'" :visible.sync="dialogFormVisible">
        <el-form :model="saveForm" :rules="rules" ref="saveForm" label-width="90px" :inline="true">
          <el-form-item label="课程学科" prop="subject">
            <el-select v-model="saveForm.subject" placeholder="请选择课程学科" clearable size="small">
              <el-option label="Java" value="0"></el-option>
              <el-option label="前端" value="1"></el-option>
              <el-option label="UI" value="2"></el-option>
              <el-option label="实施" value="3"></el-option>
              <el-option label="测试" value="4"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="课程名称" prop="name">
            <el-input v-model="saveForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="适用人群" prop="applicablePerson">
            <el-select v-model="saveForm.applicablePerson" placeholder="请选择适用人群" clearable size="small">
              <el-option label="小白学员" value="1"></el-option>
              <el-option label="中级程序员" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="课程价格" prop="price">
            <el-input-number v-model="saveForm.price" autocomplete="off" controls-position="right"></el-input-number>
          </el-form-item>
          <el-form-item label="课程介绍">
            <el-input type="textarea" v-model="saveForm.info" autocomplete="off" style="width:500px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="doSave">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dateRange: [],
      xkOption: [],
      //查询的数据模型
      searchForm: {
        //过滤条件
        code: null,
        nane: null,
        subject: null,
        applicablePerson: null,
        beginTime: null,
        endTime: null,

        //分页条件
        pageNo: 1,
        pageSize: 5,
      },
      //分页数据模型
      pageData: {
        data: [], // 分页数据
        pageNo: 1, // 当前页
        pageSize: 5, // 一页的大小
        totalCount: 0, // 总数据量
        totalPageCount: 0, // 总页数
      },
      //控制对话框显示还是隐藏
      dialogFormVisible: false,
      //新增的数据模型
      saveForm: {
        id: null,
        name: null,
        subject: null,
        applicablePerson: null,
        price: null,
        info: null,
      },
      //验证数据模型
      rules: {
        name: [
          {required: true, message: "请输入课程名称！", trigger: "blur"},
        ],
        subject: [
          {required: true, message: "请选择课程学科！", trigger: "blur"},
        ],
        applicablePerson: [
          {required: true, message: "请选择适用人群！", trigger: "blur"},
        ],
        info: [
          {required: true, message: "请输入课程介绍！", trigger: "blur"},
        ],
      },
    };
  },
  created() {
    this.doSearch();
    this.getDictDatarg();
  },
  methods: {
    //查询用户信息
    doSearch() {
      //获取开始时间和结束时间
      if (this.dateRange) {
        this.searchForm.beginTime = this.dateRange[0]
        this.searchForm.endTime = this.dateRange[1]
      } else {
        this.searchForm.beginTime = null;
        this.searchForm.endTime = null;
      }
      this.$axios.get("/api/tb/course/list", {params: this.searchForm})
          .then((res) => {
            if (res.data.code == 200) {
              this.pageData.pageNo = res.data.data.current;
              this.pageData.pageSize = res.data.data.size;
              this.pageData.totalCount = res.data.data.total;
              this.pageData.totalPageCount = res.data.data.pages;
              this.pageData.data = res.data.data.records;
            } else {
              this.$message.error(res.data.msg);
            }
          })
          .catch((err) => {
            console.log(err);
          });
    },
    //改变每页显示的条数
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.doSearch();
    },
    //改变当前的页码数
    handleCurrentChange(val) {
      this.searchForm.pageNo = val;
      this.doSearch();
    },
    //清空新增表单数据
    reset() {
      this.saveForm = {
        id: null,
        name: null,
        subject: null,
        applicablePerson: null,
        price: null,
        info: null,
      };
    },
    //添加
    handleAdd() {
      //1.清空模型数据
      this.reset();
      //2.显示对话框
      this.dialogFormVisible = true;
    },
    //保存
    doSave() {
      this.$refs["saveForm"].validate((vali) => {
        if (vali) {
          //发送ajax请求
          this.$axios.post("/api/tb/course/save", this.saveForm)
              .then((res) => {
                if (res.data.code == 200) {
                  //1.关闭对话框
                  this.dialogFormVisible = false;
                  //2.友好提示
                  this.$message.success(res.data.msg);
                  //3.刷新表格
                  this.doSearch();
                } else {
                  //1.友好提示
                  this.$message.error(res.data.msg);
                }
              })
              .catch((err) => {
                console.log(err);
              });
        }
      });
    },
    //编辑
    handleEdit(index, row) {
      //1.清空模型数据
      this.reset();
      //2.查询要修改的数据
      this.$axios.get("/api/tb/course/view/" + row.id).then((res) => {
        if (res.data.code == 200) {
          this.saveForm = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch((err) => {
        console.log(err);
      })
      //3.显示对话框
      this.dialogFormVisible = true;
    },
    //删除
    handleDelete(index, row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //1.发送ajax请求
        this.$axios.delete("/api/tb/course/del/" + row.id).then((res) => {
          if (res.data.code == 200) {
            //1.友好提示
            this.$message.success(res.data.msg);
            //2.刷新表格
            this.doSearch();
          } else {
            //1.友好提示
            this.$message.error(res.data.msg);
          }
        }).catch((err) => {
          console.log(err);
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //获取字典数据
    getDictDatarg() {
      //通过API获取字典数据
      this.$axios.get("/api/sys/dictData/getDictData?dictType=" + 'course_subject').then((res) => {
        if (res.data.code == 200) {
          this.xkOption = res.data.data;
          console.log("学科", this.xkOption)
        } else {
          this.$message.error(res.data.msg);
        }
      })
          .catch((err) => {
            console.log(err);
          });
    },
    //获取字典数据 活动状态
    getDictLabel(value, options) {
      if (!value || !options || options.length == 0) {
        return '';
      }
      const dict = options.find(item => item.dictValue === value);
      return dict ? dict.dictLabel : '';
    }
  },
}
</script>

<style scoped>
</style>

